package sdk.webview.fmc.com.fmcsdk.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;


import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import sdk.webview.fmc.com.fmcsdk.R;


/**
 * 加载数据的dialog
 */
public class FMCProgressDialog extends Dialog {

    private Context context;
    private int textViewVisible;
    private String msg = "";

    public View getView(){
        return textView;
    }

    public FMCProgressDialog(Context context) {
        super(context, R.style.loadingDialogStyle);
        this.context = context;
        textViewVisible = View.GONE;
    }

    public FMCProgressDialog(Context context, String msg) {
        super(context, R.style.loadingDialogStyle);
        this.msg = msg;
        this.context = context;
        textViewVisible = View.VISIBLE;
    }

    public FMCProgressDialog(Context context, int theme) {
        super(context, theme);
        this.context = context;
        textViewVisible = View.GONE;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_loading);
        initView();
    }
private    TextView textView;
    private void initView() {
         textView = (TextView) findViewById(R.id.tv);
        GifView loading_gifview = (GifView) findViewById(R.id.loading_gifview);
//        loading_gifview.setGifResource(R.drawable.loading);
//        loading_gifview.setImageResource(R.drawable.loading);

        RequestOptions options = new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        Glide.with(context).load(R.drawable.loading).apply(options).into(loading_gifview);

//        Glide.with(context).load(R.drawable.loading).apply(mOptions).diskCacheStrategy(DiskCacheStrategy.SOURCE)
//                .into(loading_gifview);
        textView.setText(msg);
        textView.setVisibility(textViewVisible);
        setCanceledOnTouchOutside(true);
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        attributes.alpha = 0.9f;
        getWindow().setAttributes(attributes);
        setCancelable(false);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (FMCProgressDialog.this.isShowing())
                    FMCProgressDialog.this.dismiss();
                break;
        }
        return true;
    }

    public void setText(String text) {
        this.msg = text;
        if (TextUtils.isEmpty(text)) {
            textViewVisible = View.GONE;
        } else {
            textViewVisible = View.VISIBLE;
        }
    }

    public void setText(int resource) {
        this.msg = context.getString(resource);
        textViewVisible = View.VISIBLE;
    }

    public void setMessage(String text) {
        setText(text);
    }

    public void setMessage(int resource) {
        setText(resource);
    }

    public void setTextVisibility(int visibility) {
        if ((View.VISIBLE == visibility) || (visibility == View.INVISIBLE) || (View.GONE == visibility))
            textViewVisible = visibility;
    }
}
